package platform.user.auth.token;

import lombok.Getter;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.UUID;

/**
 * 会话令牌
 *
 * @author roamer
 * @version V1.0
 * @date 2019-03-10 13:25
 */
@Getter
@NoArgsConstructor
@Document
public class SessionToken implements Serializable {
    private static final long serialVersionUID = 1L;

    /** token */
    @Id
    private UUID value;

    /** 用户ID */
    private String userId;

    /** 过期时间 */
    private DateTime expirationTime;

    /** 有效时长 */
    private Long sessionLength;

    public SessionToken(String userId, Long sessionLength) {
        value = UUID.randomUUID();
        this.userId = userId;
        this.sessionLength = sessionLength;
    }

    public String getValue() {
        return value.toString();
    }

    public boolean isExpired() {
        return this.expirationTime.isBeforeNow();
    }

    public void extendExpirationTime() {
        // 设置过期时间
        this.expirationTime = new DateTime().plus(sessionLength);
    }
}
